package com.wang.tool.event;

import com.wang.tool.util.CommonUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author wangshaopeng
 * @Date 2022/6/11
 */
public class FireResult {

    /**
     * 触发结果
     */
    private Map<Class<?>, Map<String, HandleResult>> handleResults = new HashMap<>();


    /**
     * 判别是否全成功
     *
     * @return
     */
    public boolean judgeIsAllSuccess() {
        if (CommonUtil.isEmpty(handleResults)) {
            return true;
        }
        //任何一个error为非空则为不全成功
        if (handleResults.values().stream().anyMatch(c -> c.values().stream().anyMatch(r -> r.getError() != null))) {
            return false;
        }
        return true;
    }


    /**
     * 填充结果
     *
     * @param eventType
     * @param result
     */
    public void putResult(Class<?> eventType, HandleResult result) {
        handleResults.computeIfAbsent(eventType, e -> new HashMap<>()).put(result.getId(), result);
    }

    public Map<Class<?>, Map<String, HandleResult>> getHandleResults() {
        return handleResults;
    }

    public void setHandleResults(Map<Class<?>, Map<String, HandleResult>> handleResults) {
        this.handleResults = handleResults;
    }

    @Override
    public String toString() {
        return "FireResult{" +
                "handleResults=" + handleResults +
                '}';
    }
}
